package com.KKB.web;

import com.KKB.bean.Menu;
import com.KKB.service.MenuService;
import com.KKB.service.impl.MenuServiceImpl;
import com.KKB.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/menu/menus")
public class MenuServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        if ("select".equals(method)) {
            select(req, resp);
        }else if ("findById".equals(method)){
            findById(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("findAll".equals(method)){
            findAll(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String index = req.getParameter("index");
        int pageIndex = (index == null || index.length() == 0) ? 1 : Integer.parseInt(index);
        PageUtil pageUtil = new PageUtil();

        MenuService menuService = new MenuServiceImpl();
        List<Menu> menuList = menuService.getMenuList(pageIndex, pageUtil.getPageSize());

        int total = menuService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(menuList);


        req.setAttribute("pi", pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req, resp);
    }
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        MenuService menuService = new MenuServiceImpl();
        Menu menu = menuService.findById(Integer.parseInt(mid));
        List<Menu> menuList = menuService.getMenuList(1, menuService.total());
        menuList.remove(menu);
        req.setAttribute("m1", menu);
        req.setAttribute("menuList", menuList);
        req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuName = req.getParameter("menuName");
        String upmenuId = req.getParameter("upmenuId");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        String desc = req.getParameter("desc");

        Menu menu = new Menu();
        menu.setMenuName(menuName);
        menu.setUpmenuID(Integer.parseInt(upmenuId));
        menu.setUrl(url);
        menu.setState(Integer.parseInt(state));
        menu.setDesc(desc);

        MenuService menuService = new MenuServiceImpl();
        int i = menuService.insertMenu(menu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("<script>alert('新建成功');location.href='/power/menu/menus?method=select';</script>");
        } else {
            writer.println("<script>alert('新建失败');location.href='/power/menu/menus?method=findAll';</script>");
        }
    }
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MenuService menuService = new MenuServiceImpl();
        List<Menu> menuList = menuService.getMenuList(1, menuService.total());
        req.setAttribute("menuList", menuList);
        req.getRequestDispatcher("add.jsp").forward(req, resp);
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        MenuService menuService = new MenuServiceImpl();
        int i = menuService.deleteMenu(Integer.parseInt(mid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("删除成功");
        } else {
            writer.println("删除失败");
        }
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuId = req.getParameter("menuId");
        String menuName = req.getParameter("menuName");
        String upmenuId = req.getParameter("upmenuId");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        String desc = req.getParameter("desc");

        Menu menu = new Menu();
        menu.setMenuId(Integer.parseInt(menuId));
        menu.setMenuName(menuName);
        menu.setUpmenuID(Integer.parseInt(upmenuId));
        menu.setUrl(url);
        menu.setState(Integer.parseInt(state));
        menu.setDesc(desc);

        MenuService menuService = new MenuServiceImpl();
        int i = menuService.updateMenu(menu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("<script>alert('更新成功');location.href='/power/menu/menus?method=select';</script>");
        } else {
            writer.println("<script>alert('更新失败');location.href='/power/menu/menus?method=findById&mid"+menuId+"';</script>");
        }

    }

}
